package org.unt.tree.hierarchiecheck;

import java.util.ArrayList;
import java.util.List;

public class TreeHierarchieChecker {

	public static void execute(List<String[]> matrix) {

		for (int level = 3; level > 0; level--) {

			List<String> uniques = getUniqueNodes(matrix, level);

			for (String node : uniques) {

				List<String> fathers = findFathers(matrix, level, node);

				if (fathers.size() > 1) {

					for (String father : fathers) {

						System.out.println(father + " " + node);
					}
				}
			}
		}
	}

	private static List<String> findFathers(List<String[]> matrix, int level, String node) {

		List<String> fathers = new ArrayList<String>();

		for (String[] row : matrix) {

			if (row[level].equals(node) && !fathers.contains(row[level - 1])) {

				fathers.add(row[level - 1]);
			}
		}

		return fathers;
	}

	private static List<String> getUniqueNodes(List<String[]> matrix, int level) {

		List<String> uniques = new ArrayList<String>();

		for (String[] row : matrix) {

			if (!uniques.contains(row[level])) {

				uniques.add(row[level]);
			}
		}

		return uniques;
	}
}
